package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.IAudienceRatingDao;
import com.magicode.cms.dao.IBroadPlatformAudienceRatingDao;
import com.magicode.cms.dao.IChannelDao;
import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.AudienceRating;
import com.magicode.cms.entiy.base.BroadPlatformAudienceRating;
import com.magicode.cms.entiy.base.Channel;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.service.IArticleService;
import com.magicode.cms.service.IAudienceRatingService;
import com.magicode.cms.service.IBroadPlatformAudienceRatingService;
import com.magicode.cms.service.IChannelService;
import com.magicode.common.exception.BusinessException;

@Service("serviceBroadPlatformAudienceRating")
public class BroadplatformAudienceRatingServiceImpl extends BaseServiceImpl<BroadPlatformAudienceRating,IBroadPlatformAudienceRatingDao> implements IBroadPlatformAudienceRatingService,UtilPath{

	@Autowired
	@Qualifier("daobroadPlatformAudienceRating")
	private IBroadPlatformAudienceRatingDao platformAudienceRatingDao;

	
	public IBroadPlatformAudienceRatingDao getPlatformAudienceRatingDao() {
		return platformAudienceRatingDao;
	}

	public void setPlatformAudienceRatingDao(
			IBroadPlatformAudienceRatingDao platformAudienceRatingDao) {
		this.platformAudienceRatingDao = platformAudienceRatingDao;
	}

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "programName");
		map.put(json_title, "节目名称");
		map.put("width", 180);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "channelName");
		map.put(json_title, "频道");
		map.put("width", 100);
		beaninfo.add(map);
//		map = new HashMap<String, Object>();
//		map.put(json_key, "subcribeTime");
//		map.put(json_title, "发布时间");
//		map.put("width", 90);
//		beaninfo.add(map);
//		map = new HashMap<String, Object>();
//		map.put(json_key, "status");
//		map.put(json_title, "状态");
//		map.put("width", 50);
//		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "rating");
		map.put(json_title, "收视率");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "quotas");
		map.put(json_title, "收视份额");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "source");
		map.put(json_title, "来源");
		map.put("width", 50);
		beaninfo.add(map);
//		map = new HashMap<String, Object>();
//		map.put(json_key, "shareCommentsCount");
//		map.put(json_title, "分享|评论");
//		map.put("width", 70);
//		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql ="from " +BroadPlatformAudienceRating.class.getName()+" "+entity_alias_name+" ";

		hql += " left join "+entity_alias_name+".type as ty ";
		hql += " left join "+entity_alias_name+".source as s ";
		hql += " left join "+entity_alias_name+".author as au ";
		if(null!=querywhere && !querywhere.isEmpty()){
			hql += "where 1 = 1  ";  //and "+entity_alias_name+".isContribute = 0
			if(querywhere.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+querywhere.get(field_id)+"'";
			}
			/*if(querywhere.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".title like '%"+querywhere.get("title")+"%'";
			}
			if(querywhere.containsKey("status")){
				hql = hql+" and "+entity_alias_name+".status = '"+querywhere.get("status")+"'";
			}
			if(querywhere.containsKey("channelId")){
				hql = hql+" and "+entity_alias_name+".channel.id = '"+querywhere.get("channelId")+"'";
			}
			if(querywhere.containsKey("operatelId")){
				hql = hql+" and "+entity_alias_name+".readOperate.id = '"+querywhere.get("operatelId")+"'";
			}*/
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			//hql += " order by t.createTime desc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("programName", ""+entity_alias_name+".programName as programName ");
			gridsel.put("channelName", ""+entity_alias_name+".channelName as channelName ");
			//gridsel.put("lastUpdateTime", sql_date_method+"("+entity_alias_name+".lastUpdateTime ,'"+sql_time_format+"' ) as lastUpdateTime ");
			//gridsel.put("subcribeTime", sql_date_method+"("+entity_alias_name+".subcribeTime  ,'"+sql_time_format+"' ) as subcribeTime ");
			gridsel.put("rating", ""+entity_alias_name+".rating as rating ");
			gridsel.put("quotas",""+entity_alias_name+".quotas as quotas ");
//			gridsel.put("type", "case when "+entity_alias_name+".type = 1 then '资讯' else '视频' end as type ");
			gridsel.put("source", "s.title as source ");
//			gridsel.put("shareCommentsCount", ""+entity_alias_name+".shareCount || '|' || "+entity_alias_name+".commentsCount "
//					+ "  as shareCommentsCount ");
			
			return this.queryMapDataList(hql, gridsel, start, size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public IBroadPlatformAudienceRatingDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return platformAudienceRatingDao;
	}

	@Override
	public List<BroadPlatformAudienceRating> getAudienceRatingByBroadPlatform(
			String Id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	
}
